<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Contact_us extends CI_Controller {
	
	private $publickey = "6LeOtd8SAAAAANPWbZGvIHWiZyZVH--NjX1vVCx3";
	private $privatekey = "6LeOtd8SAAAAAKRmyJw_UC99l5q6Q6KRvGEteLhQ";
	
	function __construct(){
		parent::__construct();
	}
	
	
	public function index()
	{
		
		$this->load->helper('my_captcha');
		$this->load->library('form_validation');
		
		// Get a key from https://www.google.com/recaptcha/admin/create	
		$data['publickey'] = $this->publickey;
		$data['privatekey'] = $this->privatekey;
		
		# the response from reCAPTCHA
		$resp = null;
		# the error code from reCAPTCHA, if any
		$error = null;
		
		$data['resp'] = $resp;
		$data['error'] = $error;
		
		
		if ($_POST) {
			
			

			
			
			$this->form_validation->set_error_delimiters('<div class=\"error\">', '</div>');
			$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
			$this->form_validation->set_rules('title', 'Title', 'required');
			$this->form_validation->set_rules('department', 'Department', '');
			$this->form_validation->set_rules('contactmessage', 'Message', 'required');
			$this->form_validation->set_rules('recaptcha_response_field', 'Security Code', 'required|callback_check_captcha');
			
			if ($this->form_validation->run() == TRUE)
			{
				$this->load->library('email');
				$this->email->from('admin@no-reply.com', 'Onyx Admin');
				$this->email->to('ronneltumbaga@gmail.com'); 
				
				
				//$this->Accountmodel->insert_account();
				$data['message'] = 	"Hi Admin,<br/><br/>Message Details<br/><br/>
											Email: ".$this->input->post('email')."<br/>".
											"Department: ".$this->input->post('department')."<br/>".
											"Title: ".$this->input->post('title')."<br/>".
											"Message: ".$this->input->post('contactmessage')."<br/>";
				
				$msg = $this->load->view('email_template',$data,true);
				
				$this->email->subject('Onyxfang: Inquiry');
				$this->email->message($msg);	

				$this->email->send();
				
				
				$this->email->to($this->input->post('email')); 
				$data['message'] = 	"Hi,<br/><br/>Your message already sent, wait for our customer representative to contact you.<br/><br/>Thank you";
				$msg2 = $this->load->view('email_template',$data,true);
				$this->email->subject('Onyxfang: Inquery Sent');
				$this->email->message($msg2);

				$this->email->send();
				//echo $this->email->print_debugger();
				
				//exit;

				
				
				$this->session->set_flashdata('msg_contact','Message sent.');
				redirect('contact-us');
			}
			
		}
		
		$this->template->write('title', 'Contact Us');
		$this->template->write_view('header', 'global/header');
		$this->template->write_view('content', 'contact_us',$data);
		$this->template->render();
	}
	
	function check_captcha(){
		$resp = recaptcha_check_answer ($this->privatekey,
                                        $_SERVER["REMOTE_ADDR"],
                                        $this->input->post('recaptcha_challenge_field'),//$_POST["recaptcha_challenge_field"],
                                        $this->input->post('recaptcha_response_field'));//$_POST["recaptcha_response_field"]);
			if ($resp->is_valid) {
 				return true;
			} else {
				# set the error code so that we can display it
				$this->form_validation->set_message('check_captcha', 'Security code invalid!');
				//return $data['error'] = $resp->error;
				return false;
			}
	}
}

/* End of file contact_us.php */
/* Location: ./onyxapplication/controllers/contact_us.php */